package com.java88.springboot04.mapper;

import com.java88.springboot04.entity.Emp;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface EmpMapper {
	@Results(id = "EMP-MAPPING", value = {
					@Result(property = "mgr", column = "mgr"),
					@Result(property = "deptno", column = "deptno"),
					@Result(property = "manager", column = "mgr", one = @One(select = "com.java88.springboot04.mapper.EmpMapper.findByEmpno")),
					@Result(property = "dept", column = "deptno", one = @One(select = "com.java88.springboot04.mapper.DeptMapper.findByDeptno"))
	})
	@Select("select * from emp")
	List<Emp> findAll();

	@Select("select * from emp where deptno=#{deptno}")
	@ResultMap("EMP-MAPPING")
	List<Emp> findByDeptno(int deptno);

	@Select("select * from emp where empno=#{empno}")
	Emp findByEmpno(int empno);

	@SelectKey(statement = "select max(empno)+1 from emp", keyProperty = "empno", before = true, resultType = int.class)
	@Insert("insert into emp(empno,ename,job,mgr,hiredate,sal,comm,deptno) " +
					"values(#{empno},#{ename},#{job},#{mgr},#{hiredate},#{sal},#{comm},#{deptno})")
	int save(Emp emp);

	@Update("update emp set ename=#{ename},job=#{job},mgr=#{mgr},hiredate=#{hiredate},sal=#{sal},comm=#{comm},deptno=#{deptno} where empno=#{empno}")
	int update(Emp emp);

	@Delete("delete from emp where empno=#{empno}")
	int delete(int empno);
}
